//
//  YYMDetailsListViewController.m
//  On The Load
//
//  Created by 我爱的妞 on 15/11/15.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "YYMDetailsListViewController.h"
#import "AppTools.h"
#import "YYMDetailsListCell.h"
#import "YMDetailsListModel.h"
#import "YYMListDetailsViewController.h"
#import "MBProgressHUD+show.h"

@interface YYMDetailsListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation YYMDetailsListViewController

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = NO;
    
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"更多精彩";
    _dataArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
//    self.tableView.backgroundColor = [UIColor grayColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.view addSubview:self.tableView];
    
    [self afnGetData];
}

- (void)afnGetData{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [AppTools getAFN:_url andpara:nil andBlock:^(id result) {
        NSArray *array = [result objectForKey:@"data"];
        NSLog(@"%@", result);
        for (NSDictionary *dic in array) {
            
            YMDetailsListModel *model = [[YMDetailsListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.dataArray addObject:model];
        }
        
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } andfailBlock:^(id result) {
        
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"cell";
    
    YYMDetailsListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (!cell) {
        cell = [[YYMDetailsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    YMDetailsListModel *model = [_dataArray objectAtIndex:indexPath.section];
    
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 380;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YMDetailsListModel *model = [_dataArray objectAtIndex:indexPath.row];
    
    YYMListDetailsViewController *vc = [[YYMListDetailsViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.model = model;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

@end
